home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / utility / ini10.zip / FILESPEC.C < prev    next >
Text File  |  1986-11-30  |  7KB  |  206 lines

  1. /**
  2. * module    FFC\Filespec
  3. *
  4. * purpose    Parse and Make Filespecs.
  5. *
  6. * logic     Simple string manipulation.
  7. *
  8. * cautions    All output-pointers to char arrays that are to be "filled in"
  9. *        must have their memory allocated per #defines in "filespec.h".
  10. *
  11. * includes    <ffc/filespec.h>
  12. *
  13. * copyright    (c) Fran Finnegan Associates 1986
  14. **/
  15.  
  16. #ifndef LINT_ARGS
  17. #define LINT_ARGS
  18. #endif
  19. //    #include <string.h>
  20. //    #include <ffc/const.h>
  21. //    #include <ffc/filespec.h>
  22. //    #include <ffc/string.h>
  23.  
  24. #define PATH        '\\'    /* PATH delimiter */
  25. #define MAX_SPEC_CHARS    (FILESPEC - 1)        /* should be 63 */
  26. #define MAX_PATH_CHARS    (FILESPEC_PATH - 1)    /* should be 62 */
  27. #define MAX_NAME_CHARS    (FILESPEC_NAME - 1)    /* should be 8 */
  28. #define MAX_EXT_CHARS    (FILESPEC_EXT - 2)    /* should be 3 */
  29.  
  30. static    char        mszFilespec[FILESPEC];
  31. static    char        mszNull[] = "";
  32. static    char        mszNUL[] = "NUL";
  33.  
  34. /*p*/
  35.  
  36. /**
  37. * function    MakeFilespec
  38. *
  39. * purpose    Builds a filespec from supplied components.
  40. *
  41. * logic     See commented code.
  42. *
  43. * cautions    A PATH gets suffixed to pszPath;  a '.' gets prefixed to pszExt.
  44. *        All output-pointers to char arrays that are to be "filled in"
  45. *        must have their memory allocated per #defines in "filespec.h".
  46. *
  47. * usage     pszFilespec = MakeFilespec(pszFilespec, pszDrive, pszPath,
  48. *            pszName, pszExt);
  49. *
  50. * arguments            (any of these may be NULL)
  51. *        Output:
  52. *            char    *pszFilespec;     "d:\\path\\filename.ext"
  53. *        Input:
  54. *            char    *pszDrive;     "d:", "d" or ""
  55. *            char    *pszPath;     "\\path\\", "\\", "",
  56. *                         "\\path" or "path\\"
  57. *            char    *pszName;     "filename"
  58. *            char    *pszExt;     ".ext", "ext" or ""
  59. *
  60. * returns    pszFilespec = Filespec.
  61. *
  62. * modifies    no externals.
  63. *
  64. * examples    Obvious.
  65. *
  66. * copyright    (c) Fran Finnegan Associates 1986
  67. **/
  68.  
  69. extern    char        *MakeFilespec(pszFilespec, pszDrive, pszPath, pszName,
  70.                 pszExt)
  71.  
  72.     char        *pszFilespec,
  73.             *pszDrive,
  74.             *pszPath,
  75.             *pszName,
  76.             *pszExt;
  77. {
  78. register char        *pc;
  79.  
  80. if (no pszFilespec)            /* allow Filespec to be NULL    */
  81.     pszFilespec = mszFilespec;
  82. memset(pszFilespec, Null, FILESPEC);    /* zero out Filespec        */
  83. if (no pszDrive)            /* allow Drive to be NULL    */
  84.     pszDrive = mszNull;
  85. if (no pszPath)             /* allow Path to be NULL    */
  86.     pszPath = mszNull;
  87. if (no pszName)             /* allow Name to be NULL    */
  88.     pszName = mszNull;
  89. if (no pszExt)                /* allow Ext to be NULL     */
  90.     pszExt = mszNull;
  91.                     /********** Drive ***************/
  92. pszFilespec[0] = *pszDrive;        /* begin with possible Drive    */
  93. pszFilespec[1] = ':';                   /* assume it was supplied       */
  94.                     /********** Path ****************/
  95. if (!IsStrNull(pszPath)) {        /* if a Path was supplied:    */
  96.     strncat(pszFilespec, pszPath,    /*    add Path        */
  97.         MAX_PATH_CHARS);        /*      only the valid bytes    */
  98.     pc = StrNull(pszFilespec);        /*    find end-of-string    */
  99.     if (*(pc - 1) != PATH)        /*    see if it's not PATH:   */
  100.     *pc = PATH;            /*        add PATH    */
  101.     }
  102.                     /********** Name ****************/
  103. strncat(pszFilespec, pszName,        /* add Name            */
  104.     MAX_NAME_CHARS);        /*   only the valid bytes    */
  105.                     /********** Ext *****************/
  106. if (!IsStrNull(pszExt)) {        /* if an Ext was supplied:    */
  107.     strcat(pszFilespec, ".");           /*      add '.'                 */
  108.     strncat(pszFilespec,        /*    add Ext         */
  109.         StrSkip(pszExt, "."),       /*        check for a '.':      */
  110.         MAX_EXT_CHARS);        /*      only the valid bytes    */
  111.     }
  112.                     /********** Filespec ************/
  113. if (IsStrNull(pszFilespec))        /* allow Filespec to be Null    */
  114.     strcat(pszFilespec, mszNUL);    /*    convert it to NUL:    */
  115. return (strupr(pszFilespec));        /* convert it to upper case    */
  116. }
  117.  
  118. /*p*/
  119.  
  120. /**
  121. * function    ParseFilespec
  122. *
  123. * purpose    Breaks up a filespec into its components.
  124. *
  125. * logic     See commented code.
  126. *
  127. * cautions    All output-pointers to char arrays that are to be "filled in"
  128. *        must have their memory allocated per #defines in "filespec.h".
  129. *
  130. * usage     ParseFilespec(pszFilespec, pszDrive, pszPath, pszName, pszExt);
  131. *
  132. * arguments            (any of these may be NULL)
  133. *        Input:
  134. *            char    *pszFilespec;     "d:\\path\\filename.ext"
  135. *        Output:
  136. *            char    *pszDrive;     "d:" or ""
  137. *            char    *pszPath;     "\\path", "\\" or ""
  138. *            char    *pszName;     "filename"
  139. *            char    *pszExt;     "ext" or ""
  140. *
  141. * returns    nothing.  Filespec components are passed via the arguments.
  142. *
  143. * modifies    no externals.
  144. *
  145. * examples    Obvious.
  146. *
  147. * copyright    (c) Fran Finnegan Associates 1986
  148. **/
  149.  
  150. extern    void        ParseFilespec(pszFilespec, pszDrive, pszPath, pszName,
  151.                 pszExt)
  152.  
  153.     char        *pszFilespec,
  154.             *pszDrive,
  155.             *pszPath,
  156.             *pszName,
  157.             *pszExt;
  158. {
  159. register char        *pc;
  160.  
  161. auto    char        szSpec[FILESPEC];
  162.  
  163. if (no pszFilespec)            /* allow Filespec to be NULL    */
  164.     pszFilespec = mszNUL;
  165. if (no pszDrive)            /* allow Drive to be NULL    */
  166.     pszDrive = mszFilespec;
  167. if (no pszPath)             /* allow Path to be NULL    */
  168.     pszPath = mszFilespec;
  169. if (no pszName)             /* allow Name to be NULL    */
  170.     pszName = mszFilespec;
  171. if (no pszExt)                /* allow Ext to be NULL     */
  172.     pszExt = mszFilespec;
  173. memset(pszDrive, Null, FILESPEC_DRIVE); /* zero out Drive        */
  174. memset(pszPath , Null, FILESPEC_PATH ); /* zero out Path        */
  175. memset(pszExt  , Null, FILESPEC_EXT  ); /* zero out Ext         */
  176. pszName[MAX_NAME_CHARS] =        /* end Name buffer        */
  177.  szSpec[MAX_SPEC_CHARS] = Null;     /* end Spec buffer        */
  178.                     /********** Filespec ************/
  179. pszFilespec =                /* use the upper case version    */
  180.     strupr(strncpy(szSpec,        /*   convert it to upper case    */
  181.     pszFilespec, MAX_SPEC_CHARS));    /*   only the valid bytes    */
  182.                     /********** Drive ***************/
  183. if (pszFilespec[1] == ':') {            /* if it looks like a Drive:    */
  184.     *pszDrive++ = *pszFilespec;     /*    copy Drive        */
  185.     *pszDrive = ':';                    /*      add ':'                 */
  186.     pszFilespec += 2;            /*    move past Drive     */
  187.     }
  188.                     /********** Path ****************/
  189. if (pc = strrchr(pszFilespec, PATH)) {    /* find last PATH in Filespec:    */
  190.     *pc++ = Null;            /*    end Path        */
  191.     strncpy(pszPath, pszFilespec,    /*    copy Path        */
  192.         MAX_PATH_CHARS - 1);    /*      only the valid bytes    */
  193.     if (IsStrNull(pszPath))        /*    take root into account: */
  194.     *pszPath = PATH;        /*        set root    */
  195.     pszFilespec = pc;            /*    move past Path        */
  196.     }
  197.                     /********** Name ****************/
  198. if (pc = strchr(pszFilespec, '.'))      /* find first '.' in Filespec:  */
  199.     *pc++ = Null;            /*    end Name        */
  200. strncpy(pszName, pszFilespec,        /* copy Name            */
  201.     MAX_NAME_CHARS);        /*   only the valid bytes    */
  202.                     /********** Ext *****************/
  203. if (pc)                 /* if a '.' exists:             */
  204.     strncpy(pszExt, pc, MAX_EXT_CHARS); /*    copy Ext        */
  205. }
  206.